package org.javaapps.common.web.validator;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class MyDateFormatValidator implements ConstraintValidator<MyDateFormat, Date> {
	 private Pattern pattern;
	    
	    @Override
	    public void initialize(MyDateFormat dateFormat) {
	        this.pattern = Pattern.compile(dateFormat.value());
	    }
	    @Override
	    public boolean isValid(Date date, ConstraintValidatorContext constraintContext) {
	        if(date == null) {
	            return false;
	        }
	        
	        if(this.pattern.matcher(new SimpleDateFormat("yyyy-MM-dd").format(date)).matches()) {
	            return true;
	        }
	        
	        return false;
	    }

}
